<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
          icon="el-icon-plus"
          size="small"
          type="primary"
          round @click="openDrawer('add')">
            添加面经
          </el-button>
        </div>
      </template>

      <el-table :data="list" style="width: 100%">
        <el-table-column
          prop="stem"
          label="标题"
          width="400">
        </el-table-column>
        <el-table-column
          prop="creator"
          label="作者">
        </el-table-column>
        <el-table-column
          prop="likeCount"
          label="点赞">
        </el-table-column>
        <el-table-column
          prop="views"
          label="浏览数">
        </el-table-column>
        <el-table-column
          prop="createdAt"
          label="更新时间"
          width="200">
        </el-table-column>
        <el-table-column
          label="编辑"
          width="120px">
          <template #default="{row}">
            <div class="actions">
              <i class="el-icon-view" @click="openDrawer('preview',row.id)"></i>
              <i class="el-icon-edit-outline" @click="openDrawer('edit',row.id)"></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!--
        @size-change 监听每页条数的变化
        @current-change 监听当前页的变化
        :current-page 设置当前生效的【当前页】
        :page-sizes 可供选择的每页条数下拉菜单
        :page-size 设置当前生效的【每页条数】
        layout="total, sizes, prev, pager, next, jumper"
        :total 设置【总条数】
        background 按钮底色
      -->
      <el-pagination
      background
      layout="prev, pager, next"
      :current-page="current"
      :page-size="pageSize"
      :total="total"
      @current-change="handleSizeChange" />
    </el-card>

    <!-- 抽屉区域 -->
    <el-drawer
    :visible.sync="isShowerDrawer"
    :title="drawerTitle"
    size="60%">
      <div v-if="type === 'preview'" class="article-preview">
        <h5>{{formData.stem}}</h5>
        <div v-html="formData.content"></div>
      </div>
      <el-form v-else label-width="80px" :model="formData" :rules="rules" ref="formRef">
        <el-form-item label="标题" prop="stem">
          <el-input v-model="formData.stem" />
        </el-form-item>
        <el-form-item  label="内容"  prop="content">
          <quill-editor v-model="formData.content" @blur="onContentBlur"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确认</el-button>
          <el-button @click="isShowerDrawer=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import { getArticleList, createArticle, removeArticle, getArticleDetail, updateArticle } from '@/api/article'
// import { addArticleAPI, delArticleAPI, getArticleAPI, getDetailAPI, upDadaAPI } from '@/api/article'
export default {
  components: {
    quillEditor
  },
  name: 'article-page',
  data () {
    return {
      list: [],
      current: 1,
      pageSize: 10,
      total: 0,
      isShowerDrawer: false,
      type: 'add',
      formData: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }

    }
  },
  created () {
    this.initData()
  },
  computed: {
    drawerTitle () {
      if (this.type === 'edit') return '修改面经'
      if (this.type === 'preview') return '面经预览'
      return '添加面经'
    }
  },
  methods: {
    async del (id) {
      await this.$confirm('确认删除吗', '温馨提示')
      // 删除请求
      await removeArticle(id)
      // 添加成功的提示
      this.$message.success('删除成功')
      // 处理删除当前页的最后一条
      if (this.list.length === 1 && this.current > 1) {
        this.current--
      }
      // 重新渲染
      this.initData()
    },
    async onSubmit () {
      // 1、校验（针对整个表单的校验）
      await this.$refs.formRef.validate()
      // 2、请求
      if (this.type === 'add') {
        await createArticle(this.formData)
        // 3、提示
        this.$message.success('新增成功')
      }
      if (this.type === 'edit') {
        await updateArticle(this.formData)
        // 3、提示
        this.$message.success('更新成功')
      }

      // 4、关闭抽屉
      this.isShowerDrawer = false
      this.$refs.formRef.resetFields() // 重置表单
      // 5、重新渲染 将当前页重置到第一页（新增的数据，新增到第一页去）
      this.current = 1
      this.initData()
    },

    onContentBlur () {
      this.$refs.formRef.validateField('content')
    },
    async openDrawer (type, id) {
      this.type = type

      if (type !== 'add') {
        const res = await getArticleDetail(id)
        this.formData = {
          stem: res.data.stem,
          content: res.data.content,
          id: res.data.id
        }
      }
      this.isShowerDrawer = true
    },
    async initData () {
      const res = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
      this.current = res.data.current
    },
    async handleSizeChange (val) {
      this.current = val
      this.initData()
    }
  },
  watch: {
    isShowerDrawer (newValue) {
      if (!newValue) {
        this.type !== 'preview' && this.$refs.formRef.resetFields()
        this.formData = {
          stem: '',
          content: ''
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
